//
// Created by Administrator on 2023/6/2.
//
#include "../header/New_Table.h"





New_Table::New_Table(): LDF_Widget()
    , table(new LDF_Table())
    , analysis_m_b(new Analysis_Menu_Button()), visual_m_b(new Visual_Menu_Button())
    , descripttive_dialog(new Descripttive_Dialog()), descriptive_widget(new Descriptive_Widget())
{
    this->Control_Profile();


    QObject::connect(this->analysis_m_b->descriptive, &QAction::triggered, this->descripttive_dialog, &QWidget::show);


    QObject::connect(this->descripttive_dialog->ok_button, &QPushButton::clicked, this->descripttive_dialog, &QWidget::hide);
    QObject::connect(this->descripttive_dialog->ok_button, &QPushButton::clicked, this, &New_Table::Set_Columns_num);
    QObject::connect(this->descripttive_dialog->ok_button, &QPushButton::clicked, this, &New_Table::Get_A_Columns);


    QObject::connect(this->descripttive_dialog->ok_button, &QPushButton::clicked, this->descriptive_widget, &QWidget::show);
}


New_Table::New_Table(qint32 rows, qint32 columns): LDF_Widget()
    , rows(rows), columns(columns), table(new LDF_Table(this->rows, this->columns))
    , analysis_m_b(new Analysis_Menu_Button()), visual_m_b(new Visual_Menu_Button())
    , descripttive_dialog(new Descripttive_Dialog())
{
    this->Control_Profile();


    QObject::connect(this->analysis_m_b->descriptive, &QAction::triggered, this->descripttive_dialog, &QWidget::show);


    QObject::connect(this->descripttive_dialog->ok_button, &QPushButton::clicked, this->descripttive_dialog, &QWidget::hide);
    QObject::connect(this->descripttive_dialog->ok_button, &QPushButton::clicked, this, &New_Table::Set_Columns_num);
    QObject::connect(this->descripttive_dialog->ok_button, &QPushButton::clicked, this, &New_Table::Get_A_Columns);


    QObject::connect(this->descripttive_dialog->ok_button, &QPushButton::clicked, this->descriptive_widget, &QWidget::show);
}


New_Table::~New_Table()
{
    delete table, analysis_m_b, visual_m_b;
}


void New_Table::Control_Profile()
{
    this->setWindowTitle(QString("新表格"));
    LDF_Widget::Get_Client()->setStyleSheet(QString("background: rgb(255, 255, 255)"));
    this->table->resize(this->size().width() - TABLE_WIDTH, this->size().height() - TABLE_HEIGHT);
    this->table->move(BORDER_LEFT, BORDER_TOP);
    this->table->setParent(this);


    this->analysis_m_b->setParent(this);
    this->analysis_m_b->move(5, 30);

    this->visual_m_b->setParent(this);
    this->visual_m_b->move(105, 30);


}


void New_Table::resizeEvent(QResizeEvent* event)
{
    LDF_Widget::resizeEvent(event);

    this->table->resize(this->size().width() - TABLE_WIDTH, this->size().height() - TABLE_HEIGHT);
    this->table->move(BORDER_LEFT, BORDER_TOP);
}


void New_Table::Set_Table_Size(qint32 rows, qint32 columns)
{
    this->rows = rows;
    this->columns = columns;
    this->table->Set_Item_Count(rows, columns);
}


void New_Table::Set_Read_Mode(int mode)
{
    this->read_mode = mode;
}


void New_Table::Add_Vector(QVector<QVector<QString>> QVec)
{
    this->table->Add_Vector(QVec);
}


QPoint New_Table::Get_Table_Size()
{
    return QPoint(this->rows, this->columns);
}


void New_Table::Read_CSV()
{
    QString file_name {QFileDialog::getOpenFileName()};
    QFile in_file(file_name);
    QStringList row_data;

    if (in_file.open(QIODevice::ReadOnly))
    {
        QTextStream stream_text(&in_file);
        while(!stream_text.atEnd())
        {
            row_data.push_back(stream_text.readLine());
        }


        this->rows = row_data.size();
        this->columns = row_data.at(0).split(",").size();
        QVector<QVector<QString>> rows_data(this->rows);


        for (int i {0}; i < row_data.size(); i++)
        {
            QString temp_row = row_data.at(i);

            QStringList column_data = temp_row.split(",");

            QVector<QString> columns_data(this->columns);

            for (int j {0}; j < column_data.size(); j++)
            {
                columns_data[j] = column_data.at(j);
            }
            rows_data[i] = columns_data;
        }




        QStringList table_column_headers;
        QStringList table_row_headers;
        switch (this->read_mode)
        {
            case 3:
                for(int i {1}; i < rows_data[0].size(); i++)
                {
                    table_column_headers << rows_data[0][i];
                }



                for(int i {1}; i < rows_data.size();i++)
                {
                    table_row_headers << rows_data[i][0];
                }

                this->table->Get_Model()->setHorizontalHeaderLabels(table_column_headers);
                this->table->Get_Model()->setVerticalHeaderLabels(table_row_headers);


                for(int i {1}; i < rows_data.size(); i++)
                {
                    for(int j {1}; j < rows_data[i].size(); j++)
                    {
                        this->table->Get_Model()->setItem(i - 1 , j - 1, new QStandardItem(rows_data[i][j]));

                    }
                }

                this->table->setModel(this->table->Get_Model());


                this->rows = this->rows - 1;
                this->columns = this->columns - 1;
                break;

            case 2:
                for(int i {1}; i < rows_data[0].size(); i++)
                {
                    table_column_headers << rows_data[0][i];
                }


                this->table->Get_Model()->setHorizontalHeaderLabels(table_column_headers);


                for(int i {1}; i < rows_data.size(); i++)
                {
                    for(int j {0}; j < rows_data[i].size(); j++)
                    {
                        this->table->Get_Model()->setItem(i - 1 , j , new QStandardItem(rows_data[i][j]));
                    }
                }

                this->table->setModel(this->table->Get_Model());

                this->rows = this->rows - 1;

                break;

            case 1:
                for(int i {1}; i < rows_data.size();i++)
                {
                    table_row_headers << rows_data[i][0];
                }


                for(int i {0}; i < rows_data.size(); i++)
                {
                    for(int j {1}; j < rows_data[i].size(); j++)
                    {
                        this->table->Get_Model()->setItem(i , j - 1, new QStandardItem(rows_data[i][j]));
                    }
                }

                this->columns = this->columns - 1;
                break;

            case 0:
                for(int i {0}; i < rows_data.size(); i++)
                {
                    for(int j {0}; j < rows_data[i].size(); j++)
                    {
                        this->table->Get_Model()->setItem(i , j, new QStandardItem(rows_data[i][j]));
                    }
                }
                break;
        }

    }

}



void New_Table::Get_A_Columns()
{
    QVector<double> temp_qvec(this->rows);
    for(int i {0}; i < this->rows; i++)
    {
        temp_qvec[i] = this->table->Get_Model()->data(this->table->Get_Model()->index(i, this->columns_num)).toDouble();
    }
    this->qvec_float = &(temp_qvec);
    this->descriptive_widget->Set_QVec_Data(this->qvec_float);
}


void New_Table::Set_Columns_num()
{
    this->columns_num = this->descripttive_dialog->columns_edit->text().toInt() - 1;
}

